package com.my.study.permission.tree;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 创建有序树
 *
 * @author: Carlos
 * @create: 2021/01/11 19:54
 */
public class TreeMapper {

    public static MenuResponseVo menuTree(List<MenuResponseVo> list) {
        HashMap<Long, MenuResponseVo> dataMap = new HashMap<>(16);
        for (MenuResponseVo menu : list) {
            dataMap.put(menu.getId(), menu);
        }
        MenuResponseVo root = new MenuResponseVo();
        for (Map.Entry<Long, MenuResponseVo> entry : dataMap.entrySet()) {
            MenuResponseVo menu = entry.getValue();
            if (menu.getParentId().equals(0L)) {
                root.getChildren().add(menu);
            } else {
                dataMap.get(menu.getParentId()).getChildren().add(menu);
            }
        }
        root.sortChildren();
        return root;
    }


}
